﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.IO;
using System.Runtime.InteropServices;
using System.Text;
using System.Windows.Forms;
using Assist;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using FlexCell;
using YArchitech.Controls;
using YArchitech.LIB;
using YJKCodeBase.WinForm;
using ModelChecking.ModelValidation.FireCompartment;

namespace ModelChecking.ModelValidation.AdjoinFireZone
{
	public partial class ViewNotFitWinListForm : System.Windows.Forms.Form
	{
		[DllImport("user32.dll", EntryPoint = "SendMessageA")]
		public static extern int SendMessage(IntPtr hwnd, int wMsg, IntPtr wParam, IntPtr lParam);

		[DllImport("user32.dll")]
		public static extern void keybd_event(byte bVk, byte bScan, int dwFlags, int dwExtraInfo);

		private void CancelRevitOperate()
		{
			byte bVk = 27;
			int dwFlags = 0;
			int dwFlags2 = 2;
			ViewNotFitWinListForm.keybd_event(bVk, 0, dwFlags, 0);
			ViewNotFitWinListForm.keybd_event(bVk, 0, dwFlags2, 0);
		}

		private void SetFocusToRevit()
		{
			int wMsg = 7;
			ViewNotFitWinListForm.SendMessage(this.m_hWndRevit.Handle, wMsg, IntPtr.Zero, IntPtr.Zero);
		}

        public ViewNotFitWinListForm(ExternalCommandData cmdData, WindowHandle _hWndRevit, AdjoinFireZoneHandler handler, ExternalEvent exEvent, AdjoinFireZoneCheck checkManage)
		{
			this.InitializeComponent();
			this.m_cmdData = cmdData;
			this.m_checkManage = checkManage;
			this.m_doc = cmdData.Application.ActiveUIDocument.Document;
			this.m_bIsHorData = checkManage.m_bIsHorViewPlan;
			this.m_hWndRevit = _hWndRevit;
			this.m_Handler = handler;
			this.m_ExEvent = exEvent;
			handler.SetForm(this);
		}

		private void ViewNotFitWinListForm_Load(object sender, EventArgs e)
		{
			//base.Icon = Resource.YArchitechIcon;
			this.ReadDlgData();
			if (this.m_nDlgPosX < 0 || this.m_nDlgPosX > 3000)
			{
				this.m_nDlgPosX = 0;
			}
			if (this.m_nDlgPosY < 0 || this.m_nDlgPosY > 3000)
			{
				this.m_nDlgPosY = 0;
			}
            base.Location = new System.Drawing.Point(this.m_nDlgPosX, this.m_nDlgPosY);
			this.InitCellData();
		}

		private void ViewNotFitWinListForm_FormClosed(object sender, FormClosedEventArgs e)
		{
            System.Drawing.Point location = base.Location;
			this.m_nDlgPosX = location.X;
			this.m_nDlgPosY = location.Y;
			this.SaveDlgData();
			if (this.m_ExEvent != null)
			{
				this.m_ExEvent.Dispose();
				this.m_ExEvent = null;
				this.m_Handler = null;
				base.OnFormClosed(e);
			}
			this.SetFocusToRevit();
		}

		private bool ReadDlgData()
		{
			string tempPath = Path.GetTempPath();
			string path = tempPath + "AxisDim.dat";
			if (!File.Exists(path))
			{
				return false;
			}
			FileStream fileStream = new FileStream(path, FileMode.Open);
			StreamReader streamReader = new StreamReader(fileStream);
			streamReader.BaseStream.Seek(0L, SeekOrigin.Begin);
			try
			{
				this.m_nDlgPosX = Convert.ToInt32(streamReader.ReadLine());
				this.m_nDlgPosY = Convert.ToInt32(streamReader.ReadLine());
			}
			catch
			{
			}
			streamReader.Close();
			fileStream.Close();
			return true;
		}

		public void SaveDlgData()
		{
			string tempPath = Path.GetTempPath();
			string path = tempPath + "AxisDim.dat";
			FileStream fileStream = new FileStream(path, FileMode.Create);
			StreamWriter streamWriter = new StreamWriter(fileStream);
			streamWriter.WriteLine(this.m_nDlgPosX);
			streamWriter.WriteLine(this.m_nDlgPosY);
			streamWriter.Flush();
			streamWriter.Close();
			fileStream.Close();
		}

		public void WakeUp()
		{
			this.EnableCommands(true);
		}

		private void EnableCommands(bool status)
		{
			foreach (object obj in base.Controls)
			{
                System.Windows.Forms.Control control = (System.Windows.Forms.Control)obj;
				control.Enabled = status;
			}
		}

		private void DozeOff()
		{
			this.EnableCommands(false);
		}

		public bool CheckTolInfo()
		{
			if (this.m_bIsHorData)
			{
				if (!this.m_checkManage.CheckHorViewPlan())
				{
					this.m_checkManage.m_lstCheckInfoHor.Clear();
				}
				this.m_lstCheckInfoHor = this.m_checkManage.m_lstCheckInfoHor;
			}
			else
			{
				if (!this.m_checkManage.CheckVerViewPlan())
				{
					this.m_checkManage.m_lstCheckInfoVer.Clear();
				}
				this.m_lstCheckInfoVer = this.m_checkManage.m_lstCheckInfoVer;
			}
			if (this.m_lstCheckInfoHor.Count == 0 && this.m_lstCheckInfoVer.Count == 0)
			{
				string str = this.m_bIsHorData ? "水平" : "垂直";
				string text = "您所检测的楼层中，门窗洞口的" + str + "距离全部满足规范要求！";
				if (this.m_bFirstCheck)
				{
					base.Close();
					YJKMessageBox.Information(text);
					return false;
				}
				YJKMessageBox.Information(text);
			}
			this.m_bFirstCheck = false;
			this.InitCellData();
			return true;
		}

		public void InitCellData()
		{
			if (this.gridCheckData.Rows > 1)
			{
				this.gridCheckData.Range(1, 1, this.gridCheckData.Rows - 1, this.gridCheckData.Cols - 1).DeleteByRow();
			}
			Size clientSize = this.gridCheckData.ClientSize;
			int num = clientSize.Width - clientSize.Width / 100;
			num -= clientSize.Width / 30;
			int num2 = num / 7;
			int num3 = 5;
			if (!this.m_bIsHorData)
			{
				num3++;
			}
			this.gridCheckData.Cols = num3 + 1;
			this.gridCheckData.Column(0).Width = 1;
			this.gridCheckData.Column(1).Width = (short)(num - num2 * 5);
			if (this.m_bIsHorData)
			{
				this.gridCheckData.Column(2).Width = (short)(num2 * 2);
				this.gridCheckData.Column(3).Width = (short)num2;
				this.gridCheckData.Column(4).Width = (short)num2;
				this.gridCheckData.Column(5).Width = (short)num2;
			}
			else
			{
				this.gridCheckData.Column(2).Width = (short)num2;
				this.gridCheckData.Column(3).Width = (short)num2;
				this.gridCheckData.Column(4).Width = (short)num2;
				this.gridCheckData.Column(5).Width = (short)num2;
				this.gridCheckData.Column(6).Width = (short)num2;
			}
			int num4 = 1;
			this.gridCheckData.Cell(0, num4++).Text = "楼层名称";
			this.gridCheckData.Cell(0, num4++).Text = "门窗名称";
			if (!this.m_bIsHorData)
			{
				this.gridCheckData.Cell(0, num4++).Text = "有无喷淋";
			}
			this.gridCheckData.Cell(0, num4++).Text = "规范值";
			this.gridCheckData.Cell(0, num4++).Text = "设计值";
			this.gridCheckData.Cell(0, num4++).Text = "是否信任";
			this.btnFireExchange.Enabled = false;
			if (this.m_bIsHorData)
			{
				this.Text = "水平距离监测";
				this.gridCheckData.Rows = this.m_lstCheckInfoHor.Count * 2 + 1;
				this.btnFireExchange.Enabled = (this.m_lstCheckInfoHor.Count > 0);
				this.InitDataHor();
				return;
			}
			this.Text = "垂直距离监测";
			this.gridCheckData.Rows = this.m_lstCheckInfoVer.Count * 2 + 1;
			this.btnFireExchange.Enabled = (this.m_lstCheckInfoVer.Count > 0);
			this.InitDataVer();
		}

		private void InitDataHor()
		{
			if (this.m_lstCheckInfoHor == null || this.m_lstCheckInfoHor.Count == 0)
			{
				return;
			}
			CheckPairWinDoorHorInfo checkPairWinDoorHorInfo = this.m_lstCheckInfoHor[0];
			int num = 0;
			for (int i = 0; i < this.m_lstCheckInfoHor.Count; i++)
			{
				CheckPairWinDoorHorInfo checkPairWinDoorHorInfo2 = this.m_lstCheckInfoHor[i];
				int num2 = i * 2;
				this.gridCheckData.Cell(num2 + 1, 2).Text = checkPairWinDoorHorInfo2.m_eleWinDoor1.Name;
				this.gridCheckData.Cell(num2 + 1, 2).Tag = checkPairWinDoorHorInfo2.m_eleWinDoor1.Id.ToString();
				this.gridCheckData.Cell(num2 + 2, 2).Text = checkPairWinDoorHorInfo2.m_eleWinDoor2.Name;
				this.gridCheckData.Cell(num2 + 2, 2).Tag = checkPairWinDoorHorInfo2.m_eleWinDoor2.Id.ToString();
				this.gridCheckData.Cell(num2 + 1, 3).Text = checkPairWinDoorHorInfo2.m_dStandardDist.ToString("#0") + "m";
				this.gridCheckData.Cell(num2 + 1, 3).Alignment = AlignmentEnum.CenterCenter;
                this.gridCheckData.Cell(num2 + 1, 4).ForeColor = System.Drawing.Color.Red;
				this.gridCheckData.Cell(num2 + 1, 4).Text = checkPairWinDoorHorInfo2.m_dCalDist.ToString("#0.00") + "m";
				this.gridCheckData.Cell(num2 + 1, 4).Alignment = AlignmentEnum.CenterCenter;
				this.gridCheckData.Cell(num2 + 1, 5).CellType = CellTypeEnum.CheckBox;
				this.gridCheckData.Cell(num2 + 1, 5).Text = checkPairWinDoorHorInfo2.m_bIsTrust.ToString();
				this.gridCheckData.Range(num2 + 1, 1, num2 + 2, 1).Merge();
				this.gridCheckData.Range(num2 + 1, 3, num2 + 2, 3).Merge();
				this.gridCheckData.Range(num2 + 1, 4, num2 + 2, 4).Merge();
				this.gridCheckData.Range(num2 + 1, 5, num2 + 2, 5).Merge();
				this.gridCheckData.Range(num2 + 1, 3, num2 + 2, 3).Locked = true;
				this.gridCheckData.Range(num2 + 1, 4, num2 + 2, 4).Locked = true;
				this.gridCheckData.Cell(num2 + 1, 2).Locked = true;
				this.gridCheckData.Cell(num2 + 2, 2).Locked = true;
				if (i == this.m_lstCheckInfoHor.Count - 1 || (i < this.m_lstCheckInfoHor.Count - 1 && checkPairWinDoorHorInfo2.m_strViewPlanName != this.m_lstCheckInfoHor[i + 1].m_strViewPlanName))
				{
					this.gridCheckData.Cell(num2 + 1, 1).Text = checkPairWinDoorHorInfo2.m_strViewPlanName;
					this.gridCheckData.Range(num + 1, 1, num2 + 2, 1).Merge();
					this.gridCheckData.Range(num + 1, 1, num2 + 2, 1).Locked = true;
					num = num2 + 3;
				}
			}
		}

		private void InitDataVer()
		{
			if (this.m_lstCheckInfoVer == null || this.m_lstCheckInfoVer.Count == 0)
			{
				return;
			}
			CheckPairWinDoorVerInfo checkPairWinDoorVerInfo = this.m_lstCheckInfoVer[0];
			for (int i = 0; i < this.m_lstCheckInfoVer.Count; i++)
			{
				CheckPairWinDoorVerInfo checkPairWinDoorVerInfo2 = this.m_lstCheckInfoVer[i];
				int num = i * 2;
				this.gridCheckData.Cell(num + 1, 1).Text = checkPairWinDoorVerInfo2.m_strViewPlanName1;
				this.gridCheckData.Cell(num + 2, 1).Text = checkPairWinDoorVerInfo2.m_strViewPlanName2;
				this.gridCheckData.Cell(num + 1, 2).Text = checkPairWinDoorVerInfo2.m_eleWinDoor1.Name;
				this.gridCheckData.Cell(num + 1, 2).Tag = checkPairWinDoorVerInfo2.m_eleWinDoor1.Id.ToString();
				this.gridCheckData.Cell(num + 2, 2).Text = checkPairWinDoorVerInfo2.m_eleWinDoor2.Name;
				this.gridCheckData.Cell(num + 2, 2).Tag = checkPairWinDoorVerInfo2.m_eleWinDoor2.Id.ToString();
				this.gridCheckData.Cell(num + 1, 3).Text = (checkPairWinDoorVerInfo2.m_bHasSpray ? "有喷淋" : "无喷淋");
				this.gridCheckData.Cell(num + 1, 4).Text = checkPairWinDoorVerInfo2.m_dStandardDist.ToString("#0.0") + "m";
				this.gridCheckData.Cell(num + 1, 4).Alignment = AlignmentEnum.CenterCenter;
                this.gridCheckData.Cell(num + 1, 5).ForeColor = System.Drawing.Color.Red;
				this.gridCheckData.Cell(num + 1, 5).Text = checkPairWinDoorVerInfo2.m_dCalDist.ToString("#0.00") + "m";
				this.gridCheckData.Cell(num + 1, 5).Alignment = AlignmentEnum.CenterCenter;
				this.gridCheckData.Cell(num + 1, 6).CellType = CellTypeEnum.CheckBox;
				this.gridCheckData.Cell(num + 1, 6).Text = checkPairWinDoorVerInfo2.m_bIsTrust.ToString();
				this.gridCheckData.Range(num + 1, 3, num + 2, 3).Merge();
				this.gridCheckData.Range(num + 1, 4, num + 2, 4).Merge();
				this.gridCheckData.Range(num + 1, 5, num + 2, 5).Merge();
				this.gridCheckData.Range(num + 1, 6, num + 2, 6).Merge();
				this.gridCheckData.Range(num + 1, 3, num + 2, 3).Locked = true;
				this.gridCheckData.Range(num + 1, 4, num + 2, 4).Locked = true;
				this.gridCheckData.Range(num + 1, 5, num + 2, 5).Locked = true;
				this.gridCheckData.Cell(num + 1, 1).Locked = true;
				this.gridCheckData.Cell(num + 2, 1).Locked = true;
				this.gridCheckData.Cell(num + 1, 2).Locked = true;
				this.gridCheckData.Cell(num + 2, 2).Locked = true;
			}
		}

		private void ViewNotFitWinListForm_KeyPress(object sender, KeyPressEventArgs e)
		{
			if (e.KeyChar == '\u001b')
			{
				base.Close();
			}
		}

		private void btnTrust_Click(object sender, EventArgs e)
		{
			if (this.m_bIsHorData)
			{
				for (int i = 0; i < this.m_lstCheckInfoHor.Count; i++)
				{
					bool booleanValue = this.gridCheckData.Cell(i * 2 + 1, 5).BooleanValue;
					this.m_lstCheckInfoHor[i].m_bIsTrust = booleanValue;
				}
				this.m_checkManage.m_lstCheckInfoHor = this.m_lstCheckInfoHor;
			}
			else
			{
				for (int j = 0; j < this.m_lstCheckInfoVer.Count; j++)
				{
					bool booleanValue2 = this.gridCheckData.Cell(j * 2 + 1, 6).BooleanValue;
					this.m_lstCheckInfoVer[j].m_bIsTrust = booleanValue2;
				}
				this.m_checkManage.m_lstCheckInfoVer = this.m_lstCheckInfoVer;
			}
			this.MakeRequest(RequestId.Settrust);
		}

		private void btnRenew_Click(object sender, EventArgs e)
		{
			this.MakeRequest(RequestId.Renew);
		}

		private void btnFireExchange_Click(object sender, EventArgs e)
		{
			if (this.m_bIsHorData)
			{
				if (this.m_lstCheckInfoHor.Count == 0)
				{
					return;
				}
			}
			else if (this.m_lstCheckInfoVer.Count == 0)
			{
				return;
			}
			if (this.m_dlgReplace == null)
			{
				this.m_dlgReplace = new DlgFireOutlineWinDoorWallCheck(this.m_cmdData);
				this.m_dlgReplace.InitFamilyLibDoorWins();
			}
			DlgFireOutlineWinDoorWallFamilyReplace dlgFireOutlineWinDoorWallFamilyReplace = new DlgFireOutlineWinDoorWallFamilyReplace(this.m_cmdData);
			dlgFireOutlineWinDoorWallFamilyReplace.m_lstFamilyLibDoors = this.m_dlgReplace.m_lstFamilyLibDoors;
			dlgFireOutlineWinDoorWallFamilyReplace.m_lstFamilyLibDoorsRolling = this.m_dlgReplace.m_lstFamilyLibDoorsRolling;
			dlgFireOutlineWinDoorWallFamilyReplace.m_lstFamilyLibWins = this.m_dlgReplace.m_lstFamilyLibWins;
			if (dlgFireOutlineWinDoorWallFamilyReplace.ShowDialog() == DialogResult.OK)
			{
				this.m_dlgReplace.m_strDoorClassAFiName = dlgFireOutlineWinDoorWallFamilyReplace.FireDoorClassAFiName;
				this.m_dlgReplace.m_strDoorSuperFiName = dlgFireOutlineWinDoorWallFamilyReplace.FireDoorSuperFiName;
				this.m_dlgReplace.m_strWindowFiName = dlgFireOutlineWinDoorWallFamilyReplace.FireWindowClassAFiName;
				this.MakeRequest(RequestId.Replace);
			}
		}

		public void ReplaceFamily()
		{
			if (this.m_dlgReplace != null)
			{
				this.m_dlgReplace.m_lstDefine.Clear();
				if (this.m_bIsHorData)
				{
					using (List<CheckPairWinDoorHorInfo>.Enumerator enumerator = this.m_lstCheckInfoHor.GetEnumerator())
					{
						while (enumerator.MoveNext())
						{
							CheckPairWinDoorHorInfo checkPairWinDoorHorInfo = enumerator.Current;
							if (!checkPairWinDoorHorInfo.m_eleWinDoor1.Document.IsLinked && !checkPairWinDoorHorInfo.m_eleWinDoor2.Document.IsLinked)
							{
								ViewPlan viewPlanFromName = FindCrossPointsHorFireZone.GetViewPlanFromName(checkPairWinDoorHorInfo.m_strViewPlanName, this.m_doc);
								if (!checkPairWinDoorHorInfo.m_bIsFireWinDoor1)
								{
									this.m_dlgReplace.AddDoorWinDefine(viewPlanFromName, checkPairWinDoorHorInfo.m_eleWinDoor1);
								}
								if (!checkPairWinDoorHorInfo.m_bIsFireWinDoor2)
								{
									this.m_dlgReplace.AddDoorWinDefine(viewPlanFromName, checkPairWinDoorHorInfo.m_eleWinDoor2);
								}
							}
						}
						goto IL_177;
					}
				}
				foreach (CheckPairWinDoorVerInfo checkPairWinDoorVerInfo in this.m_lstCheckInfoVer)
				{
					if (!checkPairWinDoorVerInfo.m_eleWinDoor1.Document.IsLinked && !checkPairWinDoorVerInfo.m_eleWinDoor2.Document.IsLinked)
					{
						ViewPlan viewPlanFromName2 = FindCrossPointsHorFireZone.GetViewPlanFromName(checkPairWinDoorVerInfo.m_strViewPlanName1, this.m_doc);
						ViewPlan viewPlanFromName3 = FindCrossPointsHorFireZone.GetViewPlanFromName(checkPairWinDoorVerInfo.m_strViewPlanName2, this.m_doc);
						if (!checkPairWinDoorVerInfo.m_bIsFireWinDoor1)
						{
							this.m_dlgReplace.AddDoorWinDefine(viewPlanFromName2, checkPairWinDoorVerInfo.m_eleWinDoor1);
						}
						if (!checkPairWinDoorVerInfo.m_bIsFireWinDoor2)
						{
							this.m_dlgReplace.AddDoorWinDefine(viewPlanFromName3, checkPairWinDoorVerInfo.m_eleWinDoor2);
						}
					}
				}
				IL_177:
				this.m_dlgReplace.ReplaceFamilyInstance();
			}
		}

		private void btnCancel_Click(object sender, EventArgs e)
		{
			this.SetFocusToRevit();
			this.CancelRevitOperate();
			base.Close();
		}

		private void gridCheckData_MouseDoubleClick(object sender, MouseEventArgs e)
		{
			int firstRow = this.gridCheckData.Selection.FirstRow;
			int num = this.gridCheckData.Selection.FirstCol;
			if (firstRow < 1)
			{
				return;
			}
			if (this.m_bIsHorData && num == 1)
			{
				return;
			}
			int index = (firstRow + 1) / 2 - 1;
			num = 2;
			int num2 = this.gridCheckData.Cell(firstRow, num).Tag.ToInt(0);
			ElementId elementId = new ElementId(num2);
			if (this.m_doc.GetElement(elementId) == null)
			{
				return;
			}
			UIDocument activeUIDocument = this.m_cmdData.Application.ActiveUIDocument;
			RevitVersionFuncs.ClearSelection(activeUIDocument.Selection);
			List<Element> list = new List<Element>();
			if (this.m_bIsHorData)
			{
				CheckPairWinDoorHorInfo checkPairWinDoorHorInfo = this.m_lstCheckInfoHor[index];
				list.Add(checkPairWinDoorHorInfo.m_eleWinDoor1);
				list.Add(checkPairWinDoorHorInfo.m_eleWinDoor2);
			}
			else
			{
				CheckPairWinDoorVerInfo checkPairWinDoorVerInfo = this.m_lstCheckInfoVer[index];
				list.Add(checkPairWinDoorVerInfo.m_eleWinDoor1);
				list.Add(checkPairWinDoorVerInfo.m_eleWinDoor2);
			}
			RevitVersionFuncs.SetSelection(activeUIDocument.Selection, list);
			ElementSet elementSet = new ElementSet();
			foreach (Element element in list)
			{
				elementSet.Insert(element);
			}
			activeUIDocument.ShowElements(elementSet);
		}

		private void MakeRequest(RequestId request)
		{
			this.m_Handler.Request.Make(request);
			this.m_ExEvent.Raise();
			this.DozeOff();
		}

		private void ViewNotFitWinListForm_MouseLeave(object sender, EventArgs e)
		{
			if (!base.ClientRectangle.Contains(base.PointToClient(Cursor.Position)))
			{
				this.SetFocusToRevit();
				return;
			}
			base.Focus();
		}

		private int m_nDlgPosX;

		private int m_nDlgPosY;

		private bool m_bIsHorData = true;

		private ExternalCommandData m_cmdData;

		private Document m_doc;

		public List<CheckPairWinDoorHorInfo> m_lstCheckInfoHor = new List<CheckPairWinDoorHorInfo>();

		public List<CheckPairWinDoorVerInfo> m_lstCheckInfoVer = new List<CheckPairWinDoorVerInfo>();

		public AdjoinFireZoneCheck m_checkManage;

		private bool m_bFirstCheck = true;

		private DlgFireOutlineWinDoorWallCheck m_dlgReplace;

		private WindowHandle m_hWndRevit;

		private AdjoinFireZoneHandler m_Handler;

		private ExternalEvent m_ExEvent;
	}
}
